home *** CD-ROM | disk | FTP | other *** search
/ Collection of Tools & Utilities / Collection of Tools and Utilities.iso / c / genctxt.zip / CHAP7.TXT < prev    next >
Text File  |  1987-11-21  |  18KB  |  455 lines

  1.  
  2.                       Chapter 7 - Strings and Arrays
  3.  
  4.  
  5.                              WHAT IS A STRING?
  6.  
  7.              A  string is a group of characters,  usually letters of
  8.         the  alphabet.   In order to format your printout in such  a
  9.         way that it looks nice, has meaningful titles and names, and
  10.         is  esthetically  pleasing to you and the people  using  the
  11.         output of your program,  you need the ability to output text
  12.         data.  Actually you have already been using strings, because
  13.         the second program in this tutorial,  way back in Chapter 2,
  14.         output a message that was handled internally as a string.  A
  15.         complete  definition  is  a  series  of  "char"  type   data
  16.         terminated by a NULL character, which is a zero.
  17.  
  18.              When  C  is going to use a string of data in some  way,
  19.         either  to compare it with another,  output it,  copy it  to
  20.         another string,  or whatever, the functions are set up to do
  21.         what they are called to do until a NULL, which is a zero, is
  22.         detected.
  23.  
  24.                              WHAT IS AN ARRAY?
  25.  
  26.              An array is a series of homogeneous pieces of data that
  27.         are all identical in type, but the type can be quite complex
  28.         as  we will see when we get to the chapter of this  tutorial
  29.         discussing structures.  A string is simply a special case of
  30.         an array, a series of char type data.
  31.  
  32.              The  best way to see these principles is by use  of  an
  33.         example,  so  load  the program CHRSTRG.C and display it  on
  34.         your monitor.   The first thing new is the line that defines
  35.         a "char" type of data entity.  The square brackets define an
  36.         array subscript in C, and in the case of the data definition
  37.         statement,  the  5 in the brackets defines 5 data fields  of
  38.         type  "char" all defined as the variable "name".   In the  C
  39.         language,  all subscripts start at 0 and increase by 1  each
  40.         step  up  to  the  maximum which in  this  case  is  4.   We
  41.         therefore  have  5 "char" type variables  named,  "name[0]",
  42.         "name[1]",  "name[2]",  "name[3]",  and "name[4]".  You must
  43.         keep in mind that in C, the subscripts actually go from 0 to
  44.         one   less  than  the  number  defined  in  the   definition
  45.         statement.  This is due to the original definition of C  and
  46.         these   limits  cannot  be  changed  or  redefined  by   the
  47.         programmer.
  48.  
  49.                          HOW DO WE USE THE STRING?
  50.  
  51.              The  variable  "name" is therefore a string  which  can
  52.         hold up to 5 characters, but since we need room for the NULL
  53.         terminating  character, there are actually only four  useful
  54.         characters.   To load something useful into the  string,  we
  55.         have  5 statements, each of which assigns  one  alphabetical
  56.  
  57.  
  58.                                   Page 45
  59.  
  60.  
  61.  
  62.  
  63.  
  64.  
  65.  
  66.  
  67.  
  68.                       Chapter 7 - Strings and Arrays
  69.  
  70.  
  71.         character  to  one of the string characters.   Finally,  the
  72.         last place in the string is filled with the numeral 0 as the
  73.         end indicator and the string is complete.  (A "define" would
  74.         allow us to use "NULL" instead of a zero, and this would add
  75.         greatly  to  the clarity of the program. It  would  be  very
  76.         obvious that this was a NULL and not simply a zero for  some
  77.         other purpose.) Now that we have the string, we will  simply
  78.         print  it  out  with some other string data  in  the  output
  79.         statement.
  80.  
  81.              The %s is the output definition to output a string  and
  82.         the  system  will output characters starting with the  first
  83.         one in "name" until it comes to the NULL character,  and  it
  84.         will quit.   Notice that in the "printf" statement, only the
  85.         variable  name "name" needs to be given,  with no  subscript
  86.         since  we  are  interested  in starting  at  the  beginning.
  87.         (There  is  actually another reason that only  the  variable
  88.         name  is  given without brackets.   The discussion  of  that
  89.         topic will be given in the next chapter.)
  90.  
  91.                         OUTPUTTING PART OF A STRING
  92.  
  93.              The  next "printf" illustrates that we can  output  any
  94.         single  character of the string by using the "%c" and naming
  95.         the particular character of "name" we want by including  the
  96.         subscript.   The last "printf" illustrates how we can output
  97.         part  of the string by stating the starting point by using a
  98.         subscript.   The & specifies the address of  "name[1]".   We
  99.         will  study this in the next chapter but I thought you would
  100.         benefit from a little glimpse ahead.
  101.  
  102.              This example may make you feel that strings are  rather
  103.         cumbersome  to  use since you have to set up each  character
  104.         one  at  a time.   That is an incorrect  conclusion  because
  105.         strings  are  very easy to use as we will see  in  the  next
  106.         example program.
  107.  
  108.              Compile and run this program.
  109.  
  110.                           SOME STRING SUBROUTINES
  111.  
  112.              Load  the  example program STRINGS.C for an example  of
  113.         some  ways  to use strings.  First we define  four  strings.
  114.         Next  we  come  to a new function that you  will  find  very
  115.         useful,  the "strcpy" function,  or string copy.   It copies
  116.         from  one  string  to another until it  comes  to  the  NULL
  117.         character.  Remember that the NULL is actually a "0" and  is
  118.         added to the character string by the system.  It is easy  to
  119.         remember which one gets copied to which if you think of them
  120.         like an assignment statement.  Thus if you were to say,  for
  121.         example, "x = 23;", the data is copied from the right entity
  122.  
  123.  
  124.                                   Page 46
  125.  
  126.  
  127.  
  128.  
  129.  
  130.  
  131.  
  132.  
  133.  
  134.                       Chapter 7 - Strings and Arrays
  135.  
  136.  
  137.         to the left one.  In the "strcpy" function, the data is also
  138.         copied  from  the right entity to the left,  so  that  after
  139.         execution  of  the first statement, name1 will  contain  the
  140.         string "Rosalinda", but without the double quotes, they  are
  141.         the  compiler's  way  of knowing that  you  are  defining  a
  142.         string.
  143.  
  144.              Likewise,  "Zeke"  is copied into "name2" by the second
  145.         statement,  then the "title" is copied.   The title and both
  146.         names are then printed out.   Note that it is not  necessary
  147.         for  the  defined string to be exactly the same size as  the
  148.         string it will be called upon to store,  only that it is  at
  149.         least  as long as the string plus one more character for the
  150.         NULL.
  151.  
  152.                       ALPHABETICAL SORTING OF STRINGS
  153.  
  154.              The  next function we will look at is the  "strcmp"  or
  155.         the  string  compare function.   It will return a 1  if  the
  156.         first string is larger than the second, zero if they are the
  157.         same  length  and have the same characters,  and -1  if  the
  158.         first  string  is  smaller  than the  second.   One  of  the
  159.         strings,  depending  on the result of the compare is  copied
  160.         into   the   variable  "mixed",   and   the   largest   name
  161.         alphabetically  is  printed  out.   It  should  come  as  no
  162.         surprise   to   you   that  "Zeke"  wins   because   it   is
  163.         alphabetically  larger,  length  doesn't  matter,  only  the
  164.         alphabet.  It might be wise to mention that the result would
  165.         also depend on whether the letters were upper or lower case.
  166.         There are functions available with your C compiler to change
  167.         the  case of a string to all upper or all lower case if  you
  168.         desire.   These  will be used in an example program later in
  169.         this tutorial.
  170.  
  171.                              COMBINING STRINGS
  172.  
  173.              The last four statements have another new feature,  the
  174.         "strcat",  or string concat